package vv.lines.view.swing.utils;

import vv.lines.core.IActionHandler;
import vv.lines.core.ActionId;
import vv.lines.utils.TextResources;
import vv.lines.view.swing.ViewAction;

import javax.swing.*;
import java.util.HashMap;
import java.util.Map;

import static vv.lines.core.ActionId.*;

/**
 * @author vpotapenko
 */
public class ActionHolder {

    private Map<ActionId, Action> actions = new HashMap<ActionId, Action>();

    public ActionHolder(IActionHandler handler, SwingResourceHolder resourceHolder) {
        actions.put(NEW_GAME, createNewGameAction(handler, resourceHolder));
        actions.put(EXIT, createExitAction(handler, resourceHolder));
        actions.put(ABOUT, createAboutAction(handler, resourceHolder));
        actions.put(RULES, createRulesAction(handler, resourceHolder));
    }

    private Action createRulesAction(IActionHandler handler, SwingResourceHolder resourceHolder) {
        final ViewAction action = new ViewAction(RULES, TextResources.GAME_RULES, handler);
        action.putValue(Action.SMALL_ICON, resourceHolder.getRulesIcon());
        action.putValue(Action.SHORT_DESCRIPTION, TextResources.GAME_RULES);
        return action;
    }

    private ViewAction createAboutAction(IActionHandler handler, SwingResourceHolder resourceHolder) {
        final ViewAction action = new ViewAction(ABOUT, TextResources.ABOUT, handler);
        action.putValue(Action.SMALL_ICON, resourceHolder.getAboutIcon());
        action.putValue(Action.SHORT_DESCRIPTION, TextResources.ABOUT);
        return action;
    }

    private ViewAction createExitAction(IActionHandler handler, SwingResourceHolder resourceHolder) {
        final ViewAction action = new ViewAction(EXIT, TextResources.EXIT, handler);
        action.putValue(Action.SMALL_ICON, resourceHolder.getExitIcon());
        action.putValue(Action.SHORT_DESCRIPTION, TextResources.EXIT);
        return action;
    }

    private ViewAction createNewGameAction(IActionHandler handler, SwingResourceHolder resourceHolder) {
        final ViewAction action = new ViewAction(NEW_GAME, TextResources.NEW_GAME, handler);
        action.putValue(Action.SMALL_ICON, resourceHolder.getNewGameIcon());
        action.putValue(Action.SHORT_DESCRIPTION, TextResources.NEW_GAME);
        return action;
    }

    public Action getAction(ActionId id) {
        return actions.get(id);
    }
}
